<template>
  <view
    class="address-management"
    :class="invoiceList.length < 1 && page > 1 ? 'on' : ''"
    ref="container"
  >
    <view class="line" v-if="invoiceList.length > 0">
      <image src="https://qny.yijiumy.com/line.jpg" />
    </view>
    <view class="item" v-for="(item, invoiceListIndex) in invoiceList" :key="invoiceListIndex">
      <view class="address">
        <view class="consignee">
          收货人：{{ item.invoiceName }}
          <text class="phone">{{ item.phone }}</text>
        </view>
        <view>
          邮箱：{{ item.mailbox }}
        </view>
		<view>
		  税号：{{ item.licenseNumber }}
		</view>
		<view>
		  发票类型：{{ item.invoiceType==1?'企业':'个人、非企业组织' }}
		</view>
      </view>
      <view class="operation acea-row row-between-wrapper">
        <view class="select-btn">
         
        </view>
        <view class="acea-row row-middle">
          <view @click="editInvoice(invoiceListIndex)">
            <text class="iconfont icon-bianji"></text>编辑
          </view>
          <view @click="delInvoice(invoiceListIndex)">
            <text class="iconfont icon-shanchu"></text>删除
          </view>
        </view>
      </view>
    </view>
    <Loading :loaded="loadend" :loading="loading"></Loading>
    <view class="noCommodity" v-if="invoiceList.length < 1 && page > 1">
      <view class="noPictrue">
        暂无添加发票地址
      </view>
    </view>
    <view style="height:100rpx;"></view>
    <view class="footer acea-row row-between-wrapper">
      <view class="addressBnt bg-color-red" v-if="isWechat" @click="addInvoice">
        <text class="iconfont icon-tianjiadizhi"></text>添加发票信息
      </view>
      <view class="addressBnt on bg-color-red" v-else @click="addInvoice">
        <text class="iconfont icon-tianjiadizhi"></text>添加发票信息
      </view>
      <!--<view class="addressBnt wxbnt" v-if="isWechat" @click="getAddress">-->
      <!--<text class="iconfont icon-weixin2"></text>导入微信地址-->
      <!--</view>-->
    </view>
  </view>
</template>
<style scoped lang="less">
.address-management.on {
  background-color: #fff;
  height: 100vh;
}
</style>
<script type="text/babel">
import {
  getInvoice,
  delInvoice,
} from "@/api/user";
import Loading from "@/components/Loading";
import { isWeixin } from "@/utils";
// import { openAddress } from "@/libs/wechat";

export default {
  components: {
    Loading
  },
  data() {
    return {
      page: 1,
      limit: 20,
      invoiceList: [],
      loadTitle: "",
      loading: false,
      loadend: false,
      isWechat: isWeixin()
    };
  },
  mounted: function() {
    this.InvoiceList();
  },
  onReachBottom() {
    !this.loading && this.InvoiceList();
  },
  onShow: function() {
    this.refresh();
  },
  methods: {
    refresh: function() {
      this.invoiceList = [];
      this.page = 1;
      this.loadend = false;
      this.InvoiceList();
    },
    /**
     * 获取发票列表
     *
     */
    InvoiceList: function() {
      let that = this;
      if (that.loading) return; //阻止下次请求（false可以进行请求）；
      if (that.loadend) return; //阻止结束当前请求（false可以进行请求）；
      that.loading = true;
      getInvoice({ page: that.page, limit: that.limit }).then(res => {
        that.loading = false;
        //apply();js将一个数组插入另一个数组;
        that.invoiceList.push.apply(that.invoiceList, res.data);
        that.loadend = res.data.length < that.limit; //判断所有数据是否加载完成；
        that.page = that.page + 1;
      });
    },
    /**
     * 编辑发票
     */
    editInvoice: function(index) {
		
      this.$yrouter.push({
        path: "/userPack/pages/user/invoice/AddInvoice/index",
        query: { details: JSON.stringify(this.invoiceList[index])  }
      });
    },
    /**
     * 删除发票
     */
    delInvoice: function(index) {
      let that = this;
      let invoice = this.invoiceList[index];

      delInvoice(invoice.invoiceId).then(function() {
        uni.showToast({
          title: "删除成功!",
          icon:"success",
          duration: 2000,
          complete: () => {
            that.invoiceList.splice(index, 1);
            that.$set(that, "invoiceList", that.invoiceList);
          }
        });
      });
    },

    /**
     * 新增发票信息
     */
    addInvoice: function() {
      this.$yrouter.push({
        path: "/userPack/pages/user/invoice/AddInvoice/index"
      });
    },
    getInvoice() {
     
    }
  }
};
</script>
